package com.aistar.serviceVO.impl;

import com.aistar.mapper.ContestMapper;
import com.aistar.mapper.ContestMessageMapper;
import com.aistar.mapper.CustomerMapper;
import com.aistar.pojo.Contest;
import com.aistar.pojo.ContestMessage;
import com.aistar.pojo.ContestMessageExample;
import com.aistar.pojo.Customer;
import com.aistar.pojoVO.ContestAndMessageVO;
import com.aistar.serviceVO.ContestAndMessageVOService;
import com.aistar.util.MessageUtil;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ContestAndMessageVOServiceImpl implements ContestAndMessageVOService {
    private final ContestMessageMapper contestMessageMapper;
    private final ContestMapper contestMapper;
    private final CustomerMapper customerMapper;

    @Autowired
    public ContestAndMessageVOServiceImpl(
            ContestMessageMapper contestMessageMapper,
            ContestMapper contestMapper,
            CustomerMapper customerMapper) {
        this.contestMessageMapper = contestMessageMapper;
        this.contestMapper = contestMapper;
        this.customerMapper = customerMapper;
    }

    @Override
    public ServerResponse getByCustomerId(Integer customerId) {
        ContestMessageExample example = new ContestMessageExample();
        example.createCriteria()
                .andCustomerIdEqualTo(customerId)
                .andStatusEqualTo(MessageUtil.STATUS_SUCCESS)
                .andContestMsgStartTimeIsNotNull();
        example.setOrderByClause("contest_msg_start_time desc");
        List<ContestMessage> contestMessageList = contestMessageMapper.selectByExample(example);
        if (contestMessageList != null && contestMessageList.size() > 0) {
            List<ContestAndMessageVO> contestAndMessageVOList = new ArrayList<>();
            for (ContestMessage contestMessage : contestMessageList) {
                Contest contest = contestMapper.selectByPrimaryKey(contestMessage.getContestId());
                Customer customer = customerMapper.selectByPrimaryKey(contestMessage.getCustomerId());
                contestAndMessageVOList.add(new ContestAndMessageVO(contestMessage, contest, customer));
            }
            return ServerResponse.getSuccess(contestAndMessageVOList);
        }
        return ServerResponse.getFailed();
    }
}
